#!/usr/bin/perl -l

use strict;
use warnings;

open(IN,'<','/etc/dictionaries-common/words') or die "can't open file ($!)";
my $list = [];
@$list = <IN>;
close(IN);
chomp(@$list);

my $secret_index = int rand(scalar(@$list)) + 1;
my $word = $list->[ $secret_index ];

my $guessed_index;
my $guessed_word;
LIST: for ( my $i=1; $i < scalar(@$list); $i++ ) {
	if ($list->[$i] eq $word) {
		$guessed_index = $i;
		$guessed_word = $list->[$i];
		last LIST;
	}
}

print "I calculated the magic word $word to be at $guessed_index";

if ($guessed_index == $secret_index) {
	print "that is correct";
}
else {
	print "wrong: index $secret_index contains $guessed_word";
}
